package com.d09cnpm2.group8.dao;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.d09cnpm2.group8.model.TravelLocation;

public class TravelDataSource {
    private SQLiteDatabase db;
    private TravelSQLiteHelper dbHelper;
    private String TAG = "-----------Travel data sources ------>";

    public TravelDataSource(Context context) {
	dbHelper = new TravelSQLiteHelper(context);
    }

    public void open() {
	Log.e(TAG, "opening data base");
	db = dbHelper.getWritableDatabase();
    }

    public void close() {
	dbHelper.close();
    }

    // add a new location to database------------------
    public boolean addLocation(TravelLocation locat) {
	ContentValues values = new ContentValues();
	values.put(TravelSQLiteHelper.COL_CITY, locat.city);
	values.put(TravelSQLiteHelper.COL_IMG, locat.imgLink);
	values.put(TravelSQLiteHelper.COL_DESC, locat.descript);
	values.put(TravelSQLiteHelper.COL_TYPE, locat.type);
	values.put(TravelSQLiteHelper.COL_NAME, locat.name);
	values.put(TravelSQLiteHelper.COL_LAT, locat.lat);
	values.put(TravelSQLiteHelper.COL_LONG, locat.lng);
	return db.insert(TravelSQLiteHelper.TRAVEL_TBL, null, values) > 0;
    }

    // ------------------------------------------------
    public boolean deleteLocation(TravelLocation locat) {
	return db.delete(TravelSQLiteHelper.TRAVEL_TBL,
		TravelSQLiteHelper.COL_ID + " = " + locat.id, null) > 0;
    }

    // ------------------------------------------------
    public List<TravelLocation> getAllLocations() {
	Log.e(TAG, "fetch all locations");
	List<TravelLocation> locations = new ArrayList<TravelLocation>();
	Cursor cursor = db.query(TravelSQLiteHelper.TRAVEL_TBL,
		TravelSQLiteHelper.allColumns, null, null, null, null, null);
	cursor.moveToFirst();
	while (!cursor.isAfterLast()) {
	    TravelLocation l = cursorToLocation(cursor);
	    locations.add(l);
	    cursor.moveToNext();
	}
	cursor.close();
	return locations;
    }

    // ---------------------------------------------------------
    private TravelLocation cursorToLocation(Cursor cursor) {
	TravelLocation locate = new TravelLocation();
	locate.id = cursor.getInt(0);
	locate.name = cursor.getString(1);
	locate.type = cursor.getString(2);
	locate.descript = cursor.getString(3);
	locate.lng = cursor.getDouble(4);
	locate.lat = cursor.getDouble(5);
	locate.city = cursor.getString(6);
	locate.imgLink = cursor.getString(7);
	return locate;
    }

    // -------------------------------------------------------
    public void loadInitialData(String dataString) {
	TravelLocation[] locations = null;
	Log.e(TAG, "loading initial data.....");
	try {
	    JSONObject obj = new JSONObject(dataString);
	    JSONArray array = obj.getJSONArray("locations");
	    Log.e(TAG, "json array length : " + array.length());
	    locations = new TravelLocation[array.length()];
	    for (int i = 0; i < array.length(); i++) {
		JSONObject o = (JSONObject) array.get(i);
		TravelLocation l = new TravelLocation();
		l.name = o.getString("name");
		l.descript = o.getString("descript");
		l.lat = o.getDouble("latitude");
		l.lng = o.getDouble("longtitude");
		l.type = o.getString("type");
		l.imgLink = o.getString("img");
		Log.i(TAG, l.name + " ; " + l.descript + "; " + l.lat + "; "
			+ l.lng);
		locations[i] = l;
	    }

	} catch (JSONException e) {
	    e.printStackTrace();
	}
	if (locations != null) {
	    for (int i = 0; i < locations.length; i++)
		addLocation(locations[i]);
	}
    }
}
